package com.ai.service.made.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ai.common.CommonResponse;
import com.ai.entity.made.CoordinateBO;
import com.ai.entity.made.ImageLabel;
import com.ai.mapper.made.ImageLabelMapper;
import com.ai.model.made.CgLabelDto;
import com.ai.model.made.CgLabelSpotDto;
import com.ai.model.vo.CgLabelVo;
import com.ai.service.made.IImageLabelService;
import com.ai.util.SecurityUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 图片标注 服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2025-01-06
 */
@Service
public class ImageLabelServiceImpl extends ServiceImpl<ImageLabelMapper, ImageLabel> implements IImageLabelService {

    @Override
    public CommonResponse labeling(CgLabelDto cgLabelDto) {

        //图片地址
        String photoUrl = cgLabelDto.getPhotoUrl();
        //图片标识
        String photoIdentification = cgLabelDto.getPhotoIdentification();

        List<CgLabelSpotDto> cgLabelSpotDtos = cgLabelDto.getCgLabelSpotDtos();

        if (CollectionUtil.isEmpty(cgLabelSpotDtos)){
            this.remove(new QueryWrapper<ImageLabel>()
                    .eq("photo_identification", cgLabelDto.getPhotoIdentification()));
            return CommonResponse.ok("标注成功");
        }

        //首先获取当前图片对应的所有标注信息
        List<ImageLabel> dbImageLabelList = list(new QueryWrapper<ImageLabel>().eq("photo_identification", cgLabelDto.getPhotoIdentification()));
        if (CollectionUtil.isNotEmpty(dbImageLabelList)){
            //获取db标注中的所有id
            List<String> dbLabelIdList = dbImageLabelList.stream().map(ImageLabel::getId).collect(Collectors.toList());
            List<String> dtoLabelIdList = cgLabelSpotDtos.stream()
                    // 过滤掉空值
                    .filter( cgLabelSpotDto -> !StrUtil.isEmpty(cgLabelSpotDto.getImageLabelId()))
                    .map(CgLabelSpotDto::getImageLabelId)
                    .collect(Collectors.toList());
            if (CollectionUtil.isEmpty(dtoLabelIdList)){
                //移除原本所有的标注信息
                this.removeBatchByIds(dbLabelIdList);
            }else {
                //查看哪些标注信息不存在了
                dbLabelIdList.removeAll(dtoLabelIdList);
                //移除已不存在的标注信息
                this.removeBatchByIds(dbLabelIdList);
            }
        }

        LinkedList<ImageLabel> newLabelLinkedList = new LinkedList<>();

        for (CgLabelSpotDto cgLabelSpotDto : cgLabelSpotDtos) {
            if (StrUtil.isEmpty(cgLabelSpotDto.getImageLabelId())){
                ImageLabel imageLabel = new ImageLabel();
                String label = cgLabelSpotDto.getLabel();
                if (StrUtil.isEmpty(label)){
                    continue;
                }
                List<CgLabelSpotDto.LabelCoordinatePoint> spotList = cgLabelSpotDto.getSpotList();

                if (CollectionUtil.isEmpty(spotList)){
                    continue;
                }

                String spotListString = JSON.toJSONString(spotList);
                imageLabel.setLabelType(label);
                imageLabel.setSpots(spotListString);
                imageLabel.setPhotoUrl(photoUrl);
                imageLabel.setPhotoIdentification(photoIdentification);
                imageLabel.setCreateUser(SecurityUtils.getLoginUserName());
                imageLabel.setCreateTime(LocalDateTime.now());
                imageLabel.setUpdateUser(SecurityUtils.getLoginUserName());
                imageLabel.setUpdateTime(LocalDateTime.now());

                newLabelLinkedList.add(imageLabel);
            }
        }

        //批量新增
        this.saveBatch(newLabelLinkedList);

        return CommonResponse.ok("标注成功");
    }

    @Override
    public CommonResponse getLabelInfo(CgLabelDto cgLabelDto) {

        List<CgLabelSpotDto> labelSpotDtoList = new LinkedList<>();
        List<CgLabelVo> res = new LinkedList<>();
        List<ImageLabel> dbImageLabelList = list(
                new QueryWrapper<ImageLabel>().eq("photo_identification", cgLabelDto.getPhotoIdentification()));
        if (CollectionUtil.isNotEmpty(dbImageLabelList)){
            //进行实例转换
            res = dbImageLabelList.stream().map(imageLabel -> {
//                 CgLabelSpotDto cgLabelSpotDto = new CgLabelSpotDto();
//                cgLabelSpotDto.setLabel(imageLabel.getLabelType());
//                cgLabelSpotDto.setImageLabelId(imageLabel.getId());
                List<CgLabelVo> labelCoordinatePoints = JSON.parseArray(imageLabel.getSpots(), CgLabelVo.class);
                CgLabelVo cgLabelVo = labelCoordinatePoints.get(0);
                cgLabelVo.setImageLabelId(imageLabel.getId());
                cgLabelVo.setLabel(imageLabel.getLabelType());
//                cgLabelSpotDto.setSpotList(labelCoordinatePoints);
                return cgLabelVo;
            }).collect(Collectors.toList());
        }

        return CommonResponse.ok(res);
    }
}
